package com.aistar.controller;

import com.aistar.pojo.Customer;
import com.aistar.pojo.Resume;
import com.aistar.service.ResumeService;
import com.aistar.service.impl.ResumeServiceImpl;
import com.aistar.util.DateUtil;
import com.aistar.util.ServerResponse;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "ResumeServlet",urlPatterns = "/resume")
public class ResumeServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       String op =  request.getParameter("op");
       switch (op){
           case "add":add(request,response);break;
           case "getcount":  getcount(request,response);  break;
           case "update":  update(request,response);  break;
           case "updateCustInfo":  updateCustInfo(request,response);  break;
           case "updateYearSalary":  updateYearSalary(request,response);  break;
           case "getAllResumeByCustId":  getAllResumeByCustId(request,response);  break;
           case "detail":detail(request,response);break;
           case "getAllResumeByCustIdJSON":getAllResumeByCustIdJSON(request,response);break;

       }
    }

    //根据简历id查询简历详情（ServerResponse中封装了 Resume+WorkExperience + ProjectExperience+Honor+Skill）
    private void detail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer resumeId = Integer.valueOf(request.getParameter("id"));
        //service
        ResumeService service = new ResumeServiceImpl();
        ServerResponse serverResponse = service.getResumeAllVOById(resumeId);

        System.out.println(serverResponse);
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }


    /**
     * 修改简历中用户信息
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateCustInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer resumeId = Integer.valueOf(request.getParameter("resumeId"));
        String custName = request.getParameter("custName");
        String custGender = request.getParameter("custGender"); // char
        String custBirthdayStr = request.getParameter("custBirthday"); //Date
        String custStartWorkyear = request.getParameter("custStartWorkyear"); //other2
        String custTelnoStr = request.getParameter("custTelno"); //Long
        String custWorkStatus = request.getParameter("custWorkStatus");
        String custEmail = request.getParameter("custEmail");
        String custLocation = request.getParameter("custLocation");
        String custPersonId = request.getParameter("custPersonId");


        Date custBirthday = DateUtil.string2Date(custBirthdayStr);
        Long custTelno = Long.valueOf(custTelnoStr);

        //封装成Resume
        Resume resume = new Resume();
        resume.setResumeId(resumeId);
        resume.setOther1(custName);
        resume.setResumeGender(custGender);
        resume.setResumeBirth(custBirthday);
        resume.setOther2(custStartWorkyear);
        resume.setResumeTelno(custTelno);
        resume.setResumeEmail(custEmail);
        resume.setResumeLeavingWorking(custWorkStatus);
        resume.setResumeLiveCity(custLocation);
        resume.setResumePersonid(custPersonId);
        resume.setResumeUpdateTime(new Date());

        //service
        ResumeService service = new ResumeServiceImpl();
        ServerResponse serverResponse = service.modify(resume);

        if(serverResponse.getStatus() ==1) //修改成功
            serverResponse = service.getById(resumeId);


        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }

    /**
     * 修改简历中年薪
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void updateYearSalary(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer resumeId = Integer.valueOf(request.getParameter("resumeId"));
        String salaryStr = request.getParameter("salary");
         Integer yearSalary = Integer.valueOf(salaryStr);


        //封装成Resume
        Resume resume = new Resume();
        resume.setResumeId(resumeId);
        resume.setResumeIncome(yearSalary);
        resume.setResumeUpdateTime(new Date());

        //service
        ResumeService service = new ResumeServiceImpl();
        ServerResponse serverResponse = service.modify(resume);

        if(serverResponse.getStatus() ==1) //修改成功
            serverResponse = service.getById(resumeId);


        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }






    private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer resumeId = Integer.valueOf(request.getParameter("resumeId"));
        String resumeName = request.getParameter("resumeName");

        Resume resume = new Resume();
        resume.setResumeId(resumeId);
        resume.setResumeName(resumeName);
        resume.setResumeUpdateTime(new Date());

        ResumeService service =  new ResumeServiceImpl();
        ServerResponse serverResponse = service.modify(resume);
        if(serverResponse.getStatus() ==1) { //修改成功后，需要重新查询该resume (根据主键查询)
            serverResponse = service.getById(resumeId); //此处的serverResponse 里面是 查询的家里对象resume
        }
        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));


    }
        private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String resumeName = request.getParameter("resumeName");
      /* Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
      Integer custId =  customer.getCustId();*/
            Integer  custId =2;
        Resume resume =   new Resume();
      resume.setCustId(custId);
      resume.setResumeName(resumeName);
      resume.setResumeCreateTime(new Date());
        resume.setStatus(0);
        ResumeService service =  new ResumeServiceImpl();
        ServerResponse serverResponse = service.save(resume);


        if(serverResponse.getStatus()==1){
            ServerResponse sr = service.getLatestOne(custId); //获得刚添加的简历对象

            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println(JSON.toJSONString(sr));// 返回刚添加的简历对象
        }else{
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().println(JSON.toJSONString(serverResponse));// 返回添加简历失败
        }
    }

    private void getcount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("servlet getcount >>>>>>");
       Customer customer = (Customer) request.getSession().getAttribute("loginCustomer");
        Integer custId = customer.getCustId();
        System.out.println("登录用户id:" + custId);

        ResumeService service =  new ResumeServiceImpl();
        ServerResponse serverResponse = service.getCountByCustId(custId);


        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));

    }


    //获得当前登录用户的所有简历（最多只有三个）
    private void getAllResumeByCustId(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      Customer customer = (Customer)  request.getSession().getAttribute("loginCustomer");
      Integer custId = customer.getCustId();

      ResumeService service = new ResumeServiceImpl();
     ServerResponse serverResponse =    service.getAllByCustId(custId);
        List<Resume> resumeList =( List<Resume> ) serverResponse.getData();
     request.setAttribute("resumeList",resumeList);
     request.getRequestDispatcher("/resume/all.jsp").forward(request,response);
    }


    //获得当前登录用户的所有简历（最多只有三个,返回JSON数据）
    private void getAllResumeByCustIdJSON(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Customer customer = (Customer)  request.getSession().getAttribute("loginCustomer");
        Integer custId = customer.getCustId();

        ResumeService service = new ResumeServiceImpl();
        ServerResponse serverResponse = service.getAllByCustId(custId);

        response.setContentType("text/html;charset=UTF-8");
        response.getWriter().println(JSON.toJSONString(serverResponse));
    }


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request,response);
    }
}
